package com.example.xunkaisdksample;



import com.xunkai.aidoor.xunkaisdk.ICallBack;
import com.xunkai.aidoor.xunkaisdk.SendCardUtils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
	private EditText txt_Mac;
	private Button btnOpenDoor;
	private SendCardUtils openDoor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txt_Mac = (EditText) findViewById(R.id.mac);
		btnOpenDoor = (Button) findViewById(R.id.btnOpenDoor);
		btnOpenDoor.setOnClickListener(this);
		Context context = this;
		openDoor = SendCardUtils.getInstance(context);
	}

	@Override
	public void onClick(View v) {
		String mac = String.valueOf(txt_Mac.getText());
		switch (v.getId()) {
		case R.id.btnOpenDoor:
			byte[] pass = openDoor.encryption("VEHh0rWNw20L2Js8");
			openDoor.SendOpenDoor(mac, pass, new ICallBack() {
				
				@Override
				public void onResult(int code, String msg) {
					
				}
			});
			break;
		}
	}
	
	private void printHexString(byte[] b) {
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			Log.d("TAG", hex.toUpperCase() + " ");
			System.out.print(hex.toUpperCase() + " ");
		}
	}
	
    @Override
    public void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


            int hasLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
            if ( PackageManager.PERMISSION_GRANTED != hasLocationPermission) {
                requestPermissions(new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }	
}
